//
//  HMMainViewController.swift
//  MyWeibo
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //定义一个闭包 闭包强引用控制器 控制器强引用tabbar tabbar里强引用闭包
        let composeButtonClickClosure = { [weak self] in
            
            print("点击了加号按钮")
            
            //创建一个view
            let composeView = HMComposeView()
            
            //把view添加到控制器上
            composeView.show(self)
            
//            self?.view.addSubview(composeView)
            
        }
        
        let tabBar = HMTabBar()
        
        tabBar.composeButtonClickClosure = composeButtonClickClosure
    
        setValue(tabBar, forKey: "tabBar")
        
        
        //添加控制器
        addChildViewController(HMHomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(HMMessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(HMDiscoverTableViewController(), title: "发现", imgName:"tabbar_discover")
        addChildViewController(HMProfileTableViewController(), title: "我", imgName: "tabbar_profile")
    }


    //重载方法加载控制器
    func addChildViewController(vc: UIViewController,title:String,imgName:String) {
        
        //设置标题
        vc.title = title
        //设置默认的图片和选中的图片
        vc.tabBarItem.image = UIImage(named:imgName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//设置图片渲染格式为默认
        //设置文字颜色
        vc.tabBarItem .setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        
        addChildViewController(UINavigationController(rootViewController: (vc)))
        
    }
    
    
}
